package test.test_7_16;


/*
启动2个线程，交替打印出1A2B3C4D5E6F7G
要求线程1打印数字，线程2来打印字母
请利用wait notify等待唤醒机制来实现这个需求。
 */
public class Demo01 {
    public static Object object = new Object();
    public static void main(String[] args) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (object) {
                    for(int i = 1; i <= 7; i++) {
                        System.out.println(i);
                        object.notify();

                        try {
                            object.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }
            }
        }).start();
        char[] chars = new char[]{'A','B','C','D','E','F','G'};
        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (object) {
                    for (char c : chars) {
                        object.notify();
                        System.out.println(c);
                        if(c == 'G'){
                            break;
                        }
                        try {
                            object.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }
            }
        }).start();

    }
}

